//
//  Gallerys.m
//  GalleryImageDemo
//
//  Created by ju on 14-3-5.
//  Copyright (c) 2014年 Dono. All rights reserved.
//

#import "Gallerys.h"
#import "GalleryItem.h"

#define PAGEHEIGHT 26

@implementation Gallerys{
    NSOperationQueue *queue;
    UIView *_superView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        queue = [[NSOperationQueue alloc] init];
        queue.maxConcurrentOperationCount = 2;
        
        self.bounces = NO;
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.delegate = self;
        
        //init pageControl
        CGRect pageFrame = CGRectMake(0, self.frame.origin.y +  self.frame.size.height-PAGEHEIGHT, self.frame.size.width, PAGEHEIGHT);
        _pageControl = [[UIPageControl alloc] initWithFrame:pageFrame];
        _pageControl.currentPage = 0;
        _pageControl.backgroundColor = [UIColor clearColor];
        _pageControl.pageIndicatorTintColor = [UIColor blackColor];
        _pageControl.pageIndicatorTintColor = [UIColor blackColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.userInteractionEnabled = YES;
        
        
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame images:(NSArray *)imagesArr
{
    self = [self initWithFrame:frame];
    if (self) {
        _pageControl.numberOfPages = imagesArr.count;
        self.contentSize = CGSizeMake(frame.size.width*imagesArr.count, frame.size.height);
        [self loadImages:imagesArr];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame images:(NSArray*)imagesArr superView:(UIView*)superView
{
    _superView = superView;
    self = [self initWithFrame:frame images:imagesArr];
    if (self) {
        
    }
    return self;
}

- (void)loadImages:(NSArray*)imagesArr{
    GalleryItem *galleryItem;
    CGRect frame = self.frame;
    for (int i=0,len = imagesArr.count; i<len; i++) {
        galleryItem = [[GalleryItem alloc] initWithFrame:[self offetFrameFrom:frame offsetIndex:i] imagePath:imagesArr[i] queue:queue];
        [self addSubview:galleryItem];
    }
}

- (UIPageControl*)getPageControl
{
    return _pageControl;
}

- (CGRect)offetFrameFrom:(CGRect)frame offsetIndex:(int)offsetIndex
{
    return CGRectMake(frame.size.width*offsetIndex, 0, frame.size.width, frame.size.height);
}

- (void)cancel
{
    [queue cancelAllOperations];
}


#pragma mark scrollview delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = CGRectGetWidth(self.frame);
    NSUInteger page = floor((self.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
    
    
    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
